#_*_coding:utf-8_*_

import time
import scrapy
from zhilian.items import ZhilianItem


class mapSpider(scrapy.Spider):
    name = "python"
    start_urls = ['http://www.zhaopin.com/citymap.html', ]

    def parse(self, response):
        kws = ['python','php']
        citys = ['北京', '上海', '广东', '深圳', '杭州', '武汉']
        url_arg = [(x,y) for x in citys for y in kws]
        for url in url_arg :
            url_city = 'http://sou.zhaopin.com/jobs/searchresult.ashx?jl=%s&kw=%s&sm=0&p=1'%(url[0],url[1])
            print("++++++++++++++++++++++++++++++++++++++++++++")
            print(url_city)
            yield scrapy.Request(url=url_city, callback=self.parse_jobs)

    def parse_jobs(self, response):
        job_position = response.xpath('//*[@id="JobLocation"]/@value').extract_first()
        for job in response.xpath('//table[@class="newlist"]/tr'):
            kword = job.xpath('.//td[@class="zwmc"]/div/a/b/text()').extract_first()
            if kword is not None:
                item = ZhilianItem()
                item["job_company"] = job.xpath('.//td[@class="gsmc"]/a/text()').extract_first()
                item["job_price"] = job.xpath('.//td[@class="zwyx"]/text()').extract_first()
                item["job_date"] = job.xpath('.//td[@class="gxsj"]/span/text()').extract_first()
                item["job_name"] = job.xpath('.//td[@class="zwmc"]/div/a/text()').extract_first()
                item["job_position"] = job_position
                item["job_insert_timestamp"] = [(int(round(time.time() * 1000)))]
                yield item
        job_next = response.xpath('//a[@class="next-page"]/@href').extract_first()
        print("********", job_next)
        if job_next is not None:
            yield scrapy.Request(url=job_next, callback=self.parse_jobs)
